package com.zzsy.carosshopprojects.activitys.home.mainpage.member.memberconsum.order;

import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.adapter.CouponListAdapter;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.bean.CouponChoseBean;
import com.zzsy.carosshopprojects.bean.CouponUserdBean;
import com.zzsy.carosshopprojects.bean.RedPageBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import androidx.lifecycle.MutableLiveData;

public class CouponListMode extends BaseViewMode {

    private MutableLiveData<List<CouponUserdBean>> liveData;//活动
    private MutableLiveData<CouponChoseBean> live;//完成选券

    public MutableLiveData<List<CouponUserdBean>> getliveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    public MutableLiveData<CouponChoseBean> getlive() {
        if (live == null)
            live = new MutableLiveData<>();
        return live;
    }

    private List<CouponUserdBean> newList = new ArrayList<>();

    /**
     * 获取优惠券列表 进行最优筛选
     */
    public void getCouponList(Context context, String memberId, String productId, String memberCardId, String payAmount) {
        RequestUtils.MemberRedPackedList(context, DataUtil.getInstance().getStoreId(), memberId, productId, memberCardId, new MyObserver<List<RedPageBean>>(context, false) {
            @Override
            public void onSuccess(List<RedPageBean> result) {
                showUUIDList(result, Integer.valueOf(payAmount));
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }


    private void showUUIDList(List<RedPageBean> list, int payAmount) {
        for (int i = 0; i < list.size(); i++) {
            CouponUserdBean couponUserdBean = new CouponUserdBean();
            if ((payAmount >= list.get(i).getMinimumAmount())
                    && (list.get(i).getPayType().contains("4"))
            ) {
                couponUserdBean.setCouponName(list.get(i).getCouponName());
                couponUserdBean.setEndTime(list.get(i).getTimeOut());
                couponUserdBean.setFullAmount(list.get(i).getMinimumAmount());
                couponUserdBean.setPreAmount(list.get(i).getPreAmount());
                couponUserdBean.setId(list.get(i).getId());
                couponUserdBean.setPreType(list.get(i).getProductId());
                couponUserdBean.setSuperposition(list.get(i).getSuperposition());
                couponUserdBean.setGrantSource(list.get(i).getGrantSource());
                couponUserdBean.setUseActivity(list.get(i).getUseActivity());
                couponUserdBean.setUserd(true);
                couponUserdBean.setShow(true);
                newList.add(0, couponUserdBean);
            }
        }
        /**
         * 将集合进行排序
         * */
        SortList();
    }

    private void SortList() {
        Collections.sort(newList, new PriceComparator()); // 根据价格排序
        liveData.postValue(newList);
    }

    /**
     * 将数据进行排序
     */
    static class PriceComparator implements Comparator {
        public int compare(Object object1, Object object2) {// 实现接口中的方法
            CouponUserdBean p1 = (CouponUserdBean) object1; // 强制转换
            CouponUserdBean p2 = (CouponUserdBean) object2;
            return new Double(p2.getPreAmount()).compareTo(new Double(p1.getPreAmount()));
        }
    }


    /**
     * 多选优惠券进行操作
     */
    public void setOkData(Context context, CouponListAdapter couponListAdapter) {

        /**
         * 将优惠券选中的id进行字符串拼接
         * */
        String idListStr = "";
        String isUseActivity = "";
        int preAmountInt = 0;
        int index = 0;
        int superposition = -1;
        for (int i = couponListAdapter.getListData().size(), j = 0; i > j; i--) {
            CouponUserdBean myLive = couponListAdapter.getListData().get(i - 1);
            if (myLive.isCheck() && myLive.isUserd()) {
                idListStr = idListStr + "," + myLive.getId();
                isUseActivity = isUseActivity + "," + myLive.getUseActivity();
                preAmountInt = preAmountInt + myLive.getPreAmount();
                superposition = Integer.parseInt(myLive.getSuperposition());
                index++;
            }
        }
        if (!StringUtils.isEmpty(idListStr) && !StringUtils.isEmpty(isUseActivity)) {
            String ids = idListStr.substring(1, idListStr.length());
            String isUse = isUseActivity.substring(1, isUseActivity.length());
            CouponChoseBean couponChoseBean = new CouponChoseBean();
            couponChoseBean.setIds(ids);
            couponChoseBean.setIndex(index);
            couponChoseBean.setIsUse(isUse);
            couponChoseBean.setPreAmountInt(preAmountInt);
            couponChoseBean.setSuperposition(superposition);
            live.postValue(couponChoseBean);
        } else {
            ToastUtils.showToast(context.getApplicationContext(), "请选择优惠券！");
        }

    }

    /**
     * 不使用优惠券
     */
    public void noUseCoupon() {
        CouponChoseBean couponChoseBean = new CouponChoseBean();
        couponChoseBean.setIds("");
        couponChoseBean.setIndex(newList.size());
        couponChoseBean.setIsUse("");
        couponChoseBean.setPreAmountInt(-1);
        couponChoseBean.setSuperposition(-1);
        live.postValue(couponChoseBean);
    }
}
